package com.syf.controller.RJController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syf.cmd.SelByIDCmd;
import com.syf.cmd.SoftwareReleaseCmd;
import com.syf.domain.store.BsCompanyApply;
import com.syf.domain.store.BsSmallClass;
import com.syf.domain.store.BsSoftwareRelease;
import com.syf.model.R;
import com.syf.service.BsCompanyApplyService;
import com.syf.service.BsSmallClassService;
import com.syf.service.BsSoftwareReleaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author syf
 * @create 2022-02-08 9:31
 */
@RestController
@RequestMapping("/release")
@Api(tags = "软件发布")
public class BsSoftwareReleaseController {

    @Autowired
    private BsSoftwareReleaseService bsSoftwareReleaseService;

    @Autowired
    private BsSmallClassService bsSmallClassService;

    @Autowired
    private BsCompanyApplyService bsCompanyApplyService;

    /**
     * 1.软件申请列表：
     *      whatClass = 0, softwareName=?, releaseStatus=?
     *
     * 2.厂家软件：
     *      whatClass = 1, softwareCode!=null , softwareName=? , softwareCode=?, status=? , companyId=?
     *
     * 3.平台软件：
     *      whatClass = 1,  softwareCode!=null , softwareName=? ,softwareCode=?, smallClass=? ,status=? ,orderByType(0时间，1更新时间,2下载数，3评分),isRecommend是否推荐
     *
     * 4.前台查询软件：
     *      whatClass = 1,  softwareCode!=null , softwareName=? ,status=? ,smallClass,orderByType(0时间，1更新时间,2下载数，3评分)
     */
    @PostMapping("/before/softwareList")
    @CrossOrigin
    @ApiOperation(value = "分页条件查询--应用(/发布表)列表")
    public R<Page<BsSoftwareRelease>> softwareList(@RequestBody SoftwareReleaseCmd softwareReleaseCmd){
        Page<BsSoftwareRelease> pageData =  bsSoftwareReleaseService.findByPage(softwareReleaseCmd) ;
        return R.ok(pageData) ;
    }



    @PostMapping("/addSoftApply")
    @ApiOperation(value = "发布一个应用申请")
    @CrossOrigin
    public R add(@RequestBody  BsSoftwareRelease bsSoftwareRelease){
        boolean exist = bsSoftwareReleaseService.checkExist(bsSoftwareRelease.getSoftwareSign());
        if(exist){
            return  R.fail("该软件已存在，请不要重复申请") ;
        }
        if(bsSoftwareRelease.getCompanyId() == null){
            bsSoftwareRelease.setCompanyId(2);
        }
        BsCompanyApply bsCompanyApply = bsCompanyApplyService.getById(bsSoftwareRelease.getCompanyId());
        if(bsCompanyApply != null){
            bsSoftwareRelease.setCompanyName(bsCompanyApply.getCompanyName());
        }
        bsSoftwareRelease.setMbSize(bsSoftwareRelease.getMbSize()+"MB");
        boolean save = bsSoftwareReleaseService.save(bsSoftwareRelease);
        if(save){
            return R.ok("新增成功") ;
        }
        return  R.fail("新增失败") ;
    }


    @PostMapping("/before/softDetails")
    @CrossOrigin
    @ApiOperation(value = "查看应用(申请)详细")
    public R<BsSoftwareRelease> applyDetails(@RequestBody SelByIDCmd selByIDCmd){
        BsSoftwareRelease bsSoftwareRelease =  bsSoftwareReleaseService.getById(selByIDCmd.getId()) ;
        return R.ok(bsSoftwareRelease) ;
    }


    @PostMapping("/updateSoft")
    @CrossOrigin
    @ApiOperation(value = "修改应用信息")
    public R updateCompany(@RequestBody  BsSoftwareRelease bsSoftwareRelease){
        if(bsSoftwareRelease.getSmallClass() != null){
            BsSmallClass bsSmallClass = bsSmallClassService.getById(bsSoftwareRelease.getSmallClass());
            if(bsSmallClass !=null){
                String smallClassName = bsSmallClass.getSmallClassName();
                bsSoftwareRelease.setToCategory(smallClassName);  //所属分类
            }
        }

        boolean update = bsSoftwareReleaseService.updateById(bsSoftwareRelease);
        if(update){
            return R.ok("修改成功") ;
        }
        return  R.fail("修改失败") ;
    }


    //--------------------------------前台----------------------------------------
    @PostMapping("/before/recommendList")
    @CrossOrigin
    @ApiOperation(value = "查询 编辑推荐--应用列表")
    public R<List<BsSoftwareRelease>> recommendList(){
        List<BsSoftwareRelease> list =  bsSoftwareReleaseService.recommendList() ;
        return R.ok(list) ;
    }


    @PostMapping("/before/newSoftList")
    @CrossOrigin
    @ApiOperation(value = "查询 新软推荐--应用列表")
    public R<List<BsSoftwareRelease>> newSoftList(){
        List<BsSoftwareRelease> list =  bsSoftwareReleaseService.newSoftList() ;
        return R.ok(list) ;
    }


    @PostMapping("/before/hotrRankingList")
    @CrossOrigin
    @ApiOperation(value = "查询 热门软件排行榜--应用列表")
    public R<List<BsSoftwareRelease>> rankingList(){
        List<BsSoftwareRelease> list =  bsSoftwareReleaseService.hotrRankingList() ;
        return R.ok(list) ;
    }

    @PostMapping("/before/downloadSoft")
    @CrossOrigin
    @ApiOperation(value = "应用下载--发送MQ异步队列增加下载次数")
    public R downloadSoft(@RequestBody  SelByIDCmd selByIDCmd){
        boolean update = bsSoftwareReleaseService.downloadSoftMQ(selByIDCmd);
        if(update){
            return R.ok("修改成功") ;
        }
        return  R.fail("修改失败") ;
    }

}
